package com.hy.controller;

import com.hy.common.Result;
import com.hy.entity.User;
import com.hy.service.UserService;
import com.hy.util.AliyunRealNameAuthUtils;
import com.hy.util.UserUtil;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class AppUserController {

    @Autowired
    private UserService userService;


    /**
     * 修改当前登录用户信息
     */
    @PostMapping("/updateUserinfo")
    public Result<User> updateUserinfo(@RequestBody User user) {
        try {
            return userService.updateUserinfo(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改用户信息失败");
        }
    }

    /**
     * 实名认证
     * @param realName
     * @param idCard
     * @return 包含图片URL的结果
     */
    @PostMapping("/uploadIdCard")
    public Result<?> uploadIdCard(@RequestParam("id") Integer id,
                                  @RequestParam("realName") String realName,
                                  @RequestParam("idCard") String idCard) {
        try {
            Map<String, Object> authenticate = AliyunRealNameAuthUtils.authenticate(realName, idCard);
            System.err.println(authenticate);
            if (authenticate.get("respMessage").equals("身份证信息匹配")) {
                User user = new User();
                user.setId(id);
                user.setRealName(realName);
                user.setIdCard(idCard);
                user.setRealNameStatus(1);
                user.setRealNameTime(new Date());
                this.updateUserinfo(user);
                return Result.success("实名成功!");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.error("实名认证失败：" );
    }

}
